#include <vector>
#include <list>
#include <iostream>
using namespace std;

class Iterator
{
public:
    Iterator(int *pos) : _pos(pos)
    {
    }
    Iterator &operator++()
    {
        _pos++;
        return *this;
    }
    int &operator*() const
    {
        return *_pos;
    }
    bool operator==(const Iterator &pos)
    {
        return _pos == pos._pos;
    }
    bool operator!=(const Iterator &pos)
    {
        return !(_pos == pos._pos);
    }

private:
    int *_pos;
};

class ConstIterator
{
public:
    ConstIterator(int *pos) : _pos(pos)
    {
    }
    ConstIterator &operator++()
    {
        _pos++;
        return *this;
    }
    const int &operator*() const
    {
        return *_pos;
    }
    bool operator==(const ConstIterator &pos) const
    {
        return _pos == pos._pos;
    }
    bool operator!=(const ConstIterator &pos) const
    {
        return !(_pos == pos._pos);
    }

private:
    int *_pos;
};

class DynamicArray
{
private:
    using reference = int &;
    using pointer = int *;
    using iterator = Iterator;
    using const_iterator = ConstIterator;

public:
    DynamicArray(size_t n, int val) : _data{new int[n]}, _size{n}
    {
        for (int i = 0; i < n; i++)
        {
            _data[i] = val;
        }
    }
    ~DynamicArray() { delete[] _data; }
    size_t size() { return _size; }
    reference operator[](size_t n)
    {
        return _data[n];
    }
    iterator begin()
    {
        return Iterator(_data);
    }
    iterator end()
    {
        return _data + _size;
    }
    const_iterator cbegin() const
    {
        return _data;
    }
    const_iterator cend() const
    {
        return _data + _size;
    }

private:
    int *_data;
    size_t _size;
};

int main()
{
    DynamicArray arr(3, 10); // 创建3个元素，每个都是10
    arr[0] = 20;

    // 用迭代器遍历（修改元素）
    for (auto it = arr.begin(); it != arr.end(); ++it)
    {
        *it += 5;
    }

    // 用const迭代器遍历（只读）
    for (auto it = arr.cbegin(); it != arr.cend(); ++it)
    {
        cout << *it << " "; // 应输出25 15 15
    }

    // 用范围for循环（依赖begin()和end()）
    for (int x : arr)
    {
        cout << x << " "; // 同上
    }
    return 0;
}